package MineGod;

import java.util.ArrayList;

public class Emitter {

	protected double xPos;
	protected double yPos;
	
	protected double spawnRate; //Particles per milliseconds
	
	protected ArrayList<Particle> particles = new ArrayList<Particle>();
	
	protected double particleXspeedMax;
	protected double particleXspeedMin;
	protected double particleYspeedMax;
	protected double particleYspeedMin;
	protected double particleLifeMax;
	protected double particleLifeMin;
	
	protected boolean alive;
	
	public Emitter(double x, double y, double r, double sxmax, double sxmin, double symax, double symin,
					double lmax, double lmin){
		xPos = x;
		yPos = y;
		spawnRate = r;
		particleXspeedMax = sxmax;
		particleXspeedMin = sxmin;
		particleYspeedMax = symax;
		particleYspeedMin = symin;
		particleLifeMax = lmax;
		particleLifeMin = lmin;
		alive = true;
	}
	
	public void step(int delta){
		//Spawn new particles:
		for(int i = 0; i < spawnRate*delta; i++){
			double pxSpeed = particleXspeedMin + Chunk.rand.nextDouble()*(particleXspeedMax-particleXspeedMin);
			double pySpeed = particleYspeedMin + Chunk.rand.nextDouble()*(particleYspeedMax-particleYspeedMin);
			double life = particleLifeMin + Chunk.rand.nextInt((int) particleLifeMax - (int) particleLifeMin);
			particles.add(new FireParticle(life,xPos, yPos, pxSpeed, pySpeed));
		
		}
		
		//Update particles:
		ArrayList<Particle> delList = new ArrayList<Particle>();
		for (Particle p : particles){
			p.update(delta);
			if (p.life <= 0){
				delList.add(p);
			}
		}
		//delete dead particles:
		for (Particle p : delList){
			particles.remove(p);
		}
		
		
	}
	public void setPosition(double x, double y){
		xPos = x;
		yPos = y;
	}
	public void render(){
		for(Particle p : particles){
			p.render();
		}
	}
	
}
